package com.tydic.system.controller.system;

import com.tydic.common.core.domain.R;
import com.tydic.common.core.validate.QueryGroup;
import com.tydic.common.mybatis.core.page.TableDataInfo;
import com.tydic.common.web.core.BaseController;
import com.tydic.system.domain.SysRole;
import com.tydic.system.domain.bo.QuerySysRoleBo;
import com.tydic.system.domain.bo.SysRoleBo;
import com.tydic.system.domain.vo.SysRoleVo;
import com.tydic.system.service.ISysDeptService;
import com.tydic.system.service.ISysRoleService;
import com.tydic.system.service.ISysUserService;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 角色信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/portal/role")
public class SysRoleController extends BaseController {

    private final ISysRoleService roleService;
    private final ISysUserService userService;
    private final ISysDeptService deptService;

    /**
     * 角色列表
     *
     * @param bo
     * @return
     */
    @PostMapping("/listData")
    public R<TableDataInfo<SysRoleVo>> listData(@Validated(QueryGroup.class) @RequestBody QuerySysRoleBo bo) {
        return R.ok(TableDataInfo.build(roleService.queryPage(bo)));
    }

    /**
     * 角色详情
     *
     * @param sysRole
     * @return
     */
    @GetMapping("/get")
    public R<SysRoleVo> get(SysRole sysRole) {
        return R.ok(roleService.getById(sysRole.getRoleCode()));
    }

    /**
     * 插入角色
     *
     * @param bo
     * @return
     */
    @PostMapping("/insert")
    public R<Void> insert(@RequestBody SysRoleBo bo) {
        roleService.save(bo);
        return R.ok();
    }

    /**
     * 修改角色
     *
     * @param bo
     * @return
     */
    @PostMapping("/update")
    public R<Void> update(@RequestBody SysRoleBo bo) {
        roleService.save(bo);
        return R.ok();
    }

    /**
     * 修改角色
     *
     * @param bo
     * @return
     */
    @GetMapping("/delete")
    public R<Void> delete(@NotBlank(message = "橘色编码不能为空") String roleCode) {
        roleService.deleteByRoleCode(roleCode);
        return R.ok();
    }
}
